package com.fstec.ks.master.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fstec.ks.master.MasterEngine;
import com.fstec.ks.master.bean.ExamRoundBean;
import com.fstec.ks.master.bean.ExamServerBean;
import com.sinotn.SinotnLogger;
import com.sinotn.gson.GsonFacade;
import com.sinotn.web.ResponseUtil;

/**
 * 控制台请求处理类
 * @Title ConsoleAction.java
 * @Package com.fstec.ks.master.action
 * @Description 
 * Copyright: Copyright (c) 2023
 * Company:广东峰上科技有限公司
 * 
 * @author <a href="mailto:xieqj@fstec.com">谢启进</a>
 * @date 2023年10月8日 上午10:49:28
 * @version V1.0
 */
@Controller
@RequestMapping(value = "/R/console")
public class ConsoleAction {
	@Autowired
	private MasterEngine masterEngine;
	//private List<ExamServerBean> examServers;
	//private long lastTime=0;
	
	@RequestMapping(value = "/rounds.jspx")
	public void rounds(HttpServletRequest request,HttpServletResponse response){
		StringBuilder sb=new StringBuilder();
		sb.append("{\"state\":0");
		List<ExamRoundBean> examRounds=this.masterEngine.allExamRounds();
		if(examRounds!=null){
			sb.append(",\"data\":");
			GsonFacade.toJson(this.formatRound(examRounds), sb);
		}
		sb.append("}");
		ResponseUtil.renderJson(response, sb.toString());
	}
	/*
	 * 场次数据格式化返回，已结束场次放在后面
	 */
	private List<ExamRoundBean> formatRound(List<ExamRoundBean> examRounds) {
		int size=examRounds.size();
		ArrayList<ExamRoundBean> list=new ArrayList<ExamRoundBean>(size);
		ArrayList<ExamRoundBean> listExpire=new ArrayList<ExamRoundBean>(size);
		ExamRoundBean bean;
		Date time=new Date();
		for(int i=0;i<size;i++){
			bean=examRounds.get(i);
			if(bean.getEndTime().before(time)){
				listExpire.add(bean);
			}else{
				list.add(bean);
			}
		}
		if(list.isEmpty()){
			list=listExpire;
		}else{
			if(!listExpire.isEmpty()){
				list.addAll(listExpire);
			}
		}
		return list;
	}

	@RequestMapping(value = "/servers.jspx")
	public void servers(HttpServletRequest request,HttpServletResponse response){
		String beginTimeText=request.getParameter("beginTime");
		String endTimeText=request.getParameter("endTime");
		String forceText=request.getParameter("force");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date beginTime=null;
		Date endTime=null;
		boolean force=false;
		try{
			beginTime=sdf.parse(beginTimeText);
			endTime=sdf.parse(endTimeText);
		}catch(Throwable e){
			SinotnLogger.DEBUG.error("转换场次时间字符串错误", e);
		}
		if(forceText!=null){
			force="true".equals(forceText);
		}
		List<ExamServerBean> examServers=this.masterEngine.getExamServers(beginTime, endTime, force);//this.buildExamServer(beginTime, endTime);
		StringBuilder sb=new StringBuilder();
		sb.append("{\"state\":0");
		if(examServers!=null){
			sb.append(",\"data\":");
			GsonFacade.toJson(examServers, sb);
		}
		sb.append("}");
		ResponseUtil.renderJson(response, sb.toString());
	}
	@RequestMapping(value = "/dltServer.jspx")
	public void dltServer(HttpServletRequest request,HttpServletResponse response){
		String id=request.getParameter("id");
		this.masterEngine.removeServer(id);
		ResponseUtil.renderJson(response, "{\"state\":0}");
	}
	
	/*
	private List<ExamServerBean> buildExamServer(String beginTime, String endTime) {
		File file=new File(SinotnRuntime.getExamRuntimeFolder(), "ExamServer.txt");
		long lastModifyed=file.lastModified();
		if(lastModifyed>this.lastTime){
			this.examServers=TestDataBuilder.fromFile(file, new com.google.gson.reflect.TypeToken<List<ExamServerBean>>(){}.getType());
			this.lastTime=lastModifyed;
		}
		return this.examServers;
	}
	*/
}
